﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace Sword.Utility
{
    public class IEProxyInjecter
    {
        //public struct Struct_INTERNET_PROXY_INFO
        //{
        //    public int dwAccessType;
        //    public IntPtr proxy;
        //    public IntPtr proxyBypass;
        //};

        //[DllImport("wininet.dll", SetLastError = true)]
        //private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
        //public static bool RefreshIESettings(string strProxy)
        //{
        //    const int INTERNET_OPTION_PROXY = 38;
        //    const int INTERNET_OPEN_TYPE_PROXY = 3;
        //    const int INTERNET_OPEN_TYPE_DIRECT = 1;
        //    Struct_INTERNET_PROXY_INFO struct_IPI;
        //    // Filling in structure
        //    if (string.IsNullOrEmpty(strProxy))
        //    {
        //        struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
        //    }
        //    else
        //    {
        //        struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
        //    }
        //    struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
        //    struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
        //    // Allocating memory
        //    IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
        //    // Converting structure to IntPtr
        //    Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
        //    bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));

        //    return iReturn;
        //}

        [DllImport("wininet.dll")]
        public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
        public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
        public const int INTERNET_OPTION_REFRESH = 37;


        public static void SetProxy(string proxyhost, bool proxyEnabled)
        {
            const string userRoot = "HKEY_CURRENT_USER";
            const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
            const string keyName = userRoot + "\\" + subkey;

            if (proxyEnabled)
            {
                Registry.SetValue(keyName, "ProxyServer", proxyhost);
                Registry.SetValue(keyName, "ProxyEnable", 1);
            }
            else
            {
                Registry.SetValue(keyName, "ProxyServer", "");
                Registry.SetValue(keyName, "ProxyEnable", 0);
            }

            // These lines implement the Interface in the beginning of program 
            // They cause the OS to refresh the settings, causing IP to realy update
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
        }

    }
}
